package main.java.com.example.demo.controller;

import com.example.demo.pojo.Cart;
import com.example.demo.pojo.Order;
import com.example.demo.service.CartService;
import com.example.demo.service.OrderService;
import com.example.demo.util.DateUtil;
import com.example.demo.vo.OrderVo;
import com.example.demo.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Param:
 * @Author:
 * @Date:
 */
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;

    @PostMapping("/findOrder")
    public RespBean findOrder(@RequestBody Order order){
        List<Order> orderList = orderService.findOrder(order);
        return RespBean.success("200", "查询成功", orderList);
    }

    @PostMapping("/insertOrder")
    public RespBean insertOrder(@RequestBody Cart cart){
        Order order = new Order();
        order.setGoodId(cart.getGoodId());
        order.setUserId(cart.getUserId());
        order.setCreateTime(DateUtil.getDateTimeStringYYYY(new Date()));
        orderService.insertOrder(order);
        cartService.delCart(cart);
        return RespBean.success("200", "下单成功", null);
    }

    @GetMapping("/findAllOrder")
    public RespBean findAllOrder(){
        List<OrderVo> orderList = orderService.findAllOrder();
        return RespBean.success("200", "查询成功", orderList);
    }
}
